import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/warehouse_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

class WarehouseManagementController extends GetxController {
  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 请求参数
  Map<String, dynamic> queryParameters = {};

  /// 请求参数-搜索字符串
  String search = '';

  /// 请求参数-现存情况
  String stockType = '';

  /// 智慧仓库-现存情况类型
  List<GetDictAllEntity> currentSituationType = [];

  /// 现存情况
  GetDictAllEntity currentSituation = GetDictAllEntity();

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  Future<void> initData() async {
    currentSituationType = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'xcqk_tCFXMiEm');
  }

  /// 获取产品列表数据
  Future<List<WarehouseEntity>> getWarehouseListData(
    int pageNum,
    int pageSize,
  ) async {
    queryParameters['pageNum'] = pageNum;
    queryParameters['pageSize'] = pageSize;
    queryParameters['orderType'] = 1;
    queryParameters['warehouseName'] = search;
    queryParameters['stockType'] = stockType;

    // 获取产品列表接口
    final res = await HttpUtil()
        .generateGetRequest<List<WarehouseEntity>>(
          Interface.getWarehouseByPage,
          queryParameters: queryParameters,
        )
        .execute();
    List<WarehouseEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  }

  /// 现存情况
  void showCurrentSituationSelection() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '现存情况',
      dataList: currentSituationType,
      checkedList: [currentSituation],
      isMultiple: false,
      onCallBack: (value) {
        currentSituation = value[0];
        stockType = currentSituation.getId();
        refreshPage();
        update();
      },
    );
  }

  /// 进入详情页面
  void toDetailsPage(String id) {
    Get.toNamed(AppRoutes.warehouseDetails, arguments: id);
  }

  /// 搜索刷新页面
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
